﻿using System;
using System.Linq;
using System.Web.Mvc;
using SuperStar.Models;

namespace SuperStar.Areas.Admincp.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class ProductPromotionController : Controller
    {
        private readonly ProductPromotionModels _productPromotionModels = new ProductPromotionModels();

        public void Debugger()
        {
            if (Session["product_debugger"] == null)
            {
                Session.Add("product_debugger", "0");
            }
            if (Request.Params["product_debugger"] != null)
            {
                Session["product_debugger"] = Request.Params["product_debugger"];
            }
            _productPromotionModels.DEBUGTYPE
                   = Session["product_debugger"].ToString();
        }

        public ActionResult GetLists(int productId)
        {
            try
            {
                var productPromotions = _productPromotionModels.GetProductPromotions(productId)
                               .Select(p => new {
                               Id = p.Id,
                               p.Name,
                               p.GiftPointRequired,
                               p.MaxGiftAmount,
                               p.RemainAmount,
                               p.Quantity,
                               p.GiftOfferedCount,
                               p.ProductId
                       });
                var result = new {
                                     Result = "OK",
                                     Records = productPromotions
                                 };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
       
        public ActionResult Update(ProductPromotion productPromotion)
        {
            Debugger();
            object result;
            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = string.Join("<br/>", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                    return Json(new { Result = "ERROR", Message = "Lỗi trong quá trình sửa:<br/>" + errors });
                }
                if (_productPromotionModels.Update(productPromotion))
                    result = new { Result = "OK" };
                else
                    result = new { Result = "ERROR", Message = "Có lỗi trong quá trình cập nhật!" };
               
            }
            catch (Exception ex)
            {
                result = new { Result = "ERROR", Message = ex.InnerException.Message};
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Create(ProductPromotion productPromotion)
        {
            object result;
            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = string.Join("<br/>", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                    return Json(new { Result = "ERROR", Message = "Lỗi trong quá trình thêm:<br/>" + errors });
                }


                var promotion = _productPromotionModels.Insert(productPromotion);
                
                if(promotion != null)
                {
                    var record = new
                    {
                        Id = promotion.Id,
                        promotion.Name,
                        promotion.GiftPointRequired,
                        promotion.MaxGiftAmount,
                        promotion.RemainAmount,
                        promotion.Quantity,
                        promotion.GiftOfferedCount,
                        promotion.ProductId
                    };
                    result = new { Result = "OK", Record = record };
                }
                else
                    result = new { Result = "ERROR", Message = "Có lỗi trong quá trình thêm!" };

            }
            catch (Exception ex)
            {
                result = new { Result = "ERROR", Message = ex.Message + ex.InnerException };
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult Delete(int id)
        {
            try
            {
                _productPromotionModels.Delete(id);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
    }
}
